/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package remoting.service;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

/**
 *
 * @author cubika
 */
@ManagedBean
@ApplicationScoped
public class FlexConfigurationManager {

    public static void findAnnotation() {

    }
   

    List<String> getAnnotatedDestinations() throws IOException, ClassNotFoundException {
//      FacesContext context = FacesContext.getCurrentInstance();
//
//        Map<String,Object> sessionMap = context.getExternalContext().getSessionMap();
  Set<RemotingDestinationMetadata> remotingDestinations = new HashSet<RemotingDestinationMetadata>();
        for ( Iterator<Class> it = getClasses("com").iterator(); it.hasNext();) {
            Class clazz = it.next();
            RemotingDestination remotingDestination = null;
            remotingDestination = (RemotingDestination) clazz.getAnnotation(RemotingDestination.class);
          if(remotingDestination!=null){
            remotingDestinations.add(new RemotingDestinationMetadata(remotingDestination, clazz.getSimpleName().toLowerCase(), clazz));
          }
            
        }
        List list = new ArrayList<String>();
        for (RemotingDestinationMetadata rdm : remotingDestinations) {
            list.add(rdm.getBeanName()+"Destination");
            System.out.println(rdm.getBeanName());
        }
        return list;
    }

    private Iterable<Class> getClasses(String pack) throws IOException, ClassNotFoundException {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        String path = pack.replace('.', File.separator.charAt(0));
        Enumeration<URL> resources = cl.getResources(path);
        List<File> dirs = new ArrayList<File>();


        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            dirs.add(new File(url.getFile()));
        }
        List<Class> classes = new ArrayList<Class>();
        for (File directory : dirs) {
            classes.addAll(findClasses(directory, pack));
        }
return classes;

    }
    private List<Class> findClasses(File directory, String pack) throws ClassNotFoundException{
        List<Class> classes = new ArrayList<Class>();
        if(!directory.exists()){
            return classes;
        }
        File[] files = directory.listFiles();
        for (File file : files) {
            if(file.isDirectory()){
                classes.addAll(findClasses(file, pack + "." + file.getName()));
            }
            else if(file.getName().endsWith(".class")){
                classes.add(Class.forName(pack+"."+file.getName().substring(0,file.getName().length()-6)));
            }
        }
        return classes;
    }



}
